package www.rlicker.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import www.rlicker.model.Feedback;
import www.rlicker.mybatis.Criteria;
import www.rlicker.service.IFeedbackService;

import java.util.List;

import jersey.repackaged.com.google.common.collect.Lists;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

@Service
public class SendNotificationService {

	@Autowired
	private IFeedbackService service;

	private static JPushClient jpushClient = null;

	private final static String MASTER_SECRET = "387f3806b96f5fcecb1e3546";

	private final static String APP_KEY = "39bf1fc8062bc65698364aac";

	static {
		jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
	}


	/**
	 * 删除别名
	 */
	public void deleteAlias(String alias) {
		try {
			jpushClient.deleteAlias(alias, null);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送给指定用户
	 */
	public void sendToUser(Long userId, String type, String msg) {


		PushPayload pushPayload = PushPayload.newBuilder()
			.setPlatform(Platform.all())
			.setAudience(Audience.alias(userId + ""))
			.setNotification(Notification.newBuilder()
				.setAlert(msg)
				.addPlatformNotification(IosNotification.newBuilder()
					.setSound("default")
					.addExtra("type", type)
					.build())
				.addPlatformNotification(AndroidNotification.newBuilder()
					// 通知提醒方式
					.addExtra("type", type)
					// 通知栏样式类型
					.setStyle(2)
					.setBuilderId(1)
					.build())
				.build())
			.setMessage(Message.newBuilder()
				.addExtra("type", type)
				.setContentType("text")
				.setMsgContent(msg)
				.build())
			.build();
		try {
			// 发送信息
			PushResult pushResult = jpushClient.sendPush(pushPayload);
			System.out.println(pushResult);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}


	/**
	 * 发送给指定设备
	 */
	public void sendGroupAllUser(Long groupId, String type, String msg) {
		Criteria<Feedback> param = new Criteria<Feedback>();
		List<Feedback> agentUserVo = service.queryPageList(param);
		List<String> list = Lists.newArrayList();
		agentUserVo.forEach(f -> {
			Long id =  new Long(f.getUserId());
			list.add(id + "");
		});
		PushPayload pushPayload = PushPayload.newBuilder()
			.setPlatform(Platform.all())
			.setAudience(Audience.alias(list))
			.setNotification(Notification.newBuilder()
				.setAlert(msg)
				.addPlatformNotification(IosNotification.newBuilder()
					.setSound("default")
					.addExtra("type", type)
					.addExtra("groupId", groupId)
					.build())
				.addPlatformNotification(AndroidNotification.newBuilder()
					// 通知提醒方式
					.addExtra("type", type)
					.addExtra("groupId", groupId)
					// 通知栏样式类型
					.setStyle(2)
					.setBuilderId(1)
					.build())
				.build())
			.setMessage(Message.newBuilder()
				.addExtra("type", type)
				.addExtra("groupId", groupId)
				.setContentType("text")
				.setMsgContent(msg)
				.build())
			.build();
		try {
			// 发送信息
			jpushClient.sendPush(pushPayload);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}

	}


}


